package com.neusoft.nepm.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.neusoft.nepm.mapper.AqiFeedbackMapper;
import com.neusoft.nepm.po.AqiFeedback;
import com.neusoft.nepm.service.IAqiFeedbackService;
import com.neusoft.nepm.utils.StringUtils;
import com.neusoft.nepm.vo.ResponseData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zouran
 * createDate：2023/6/19  18:09
 */
@Service
public class AqiFeedbackServiceImpl implements IAqiFeedbackService {
    @Resource
    private AqiFeedbackMapper mapper;
    @Resource
    private StringUtils stringUtils;
    /********************** neps工程 **********************/
    @Override
    public ResponseData saveAqiFeedback(AqiFeedback aqiFeedback) {
        ResponseData data = new ResponseData<>();
        int i = mapper.insert(aqiFeedback);
        if(i==0){
            data.setCode(2001);
            data.setMessage("新增空气质量失败");
            return data;
        }
        data.setCode(200);
        data.setMessage("success");
        return data;
    }

    //根据手机号查询反馈列表
    @Override
    public ResponseData<List<AqiFeedback>> listAqiFeedbackByTelId(String telId) {
        ResponseData<List<AqiFeedback>> data = new ResponseData<>();
        //验证手机==null 或者 手机号格式不正确
        if(stringUtils.isEmpty(telId)||!stringUtils.isRealPhone(telId)){
            //数据异常
            data.setCode(1001);
            data.setMessage("手机号格式不正确");
            return  data;
        }
        if(!Objects.equals(StpUtil.getLoginIdByToken(StpUtil.getTokenValue()),telId))
        {
            data.setCode(1001);
            data.setMessage("权限不足");
            return data;
        }
        List<AqiFeedback> list=new ArrayList<>();
        //业务查询
        list = mapper.listAqiFeedbackByTelId(telId);
        if(list==null){
            //数据异常
            data.setCode(2001);
            data.setMessage("查无信息");
            return  data;
        }
        //正常返回
        data.setCode(200);
        data.setMessage("success");
        data.setResult(list);
        return  data;
    }
}
